/**
 * 
 */
package com.java.test.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.java.test.controller.BaseController;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.timer.MessagePushTimer;
import com.java.test.user.dto.*;
import com.java.test.user.service.MemberService;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/gameuser")
public class GameAccountController extends BaseController {

	@Autowired
	private MemberService memberservice;

	@RequestMapping("/bankList")
	public JsonResult bankList(@RequestBody PageInfo<BankAccountInfo> page) {
		BankAccountInfo info = page.getInfo();
		if (info == null)
			info = new BankAccountInfo();
		JsonResult js = new JsonResult();
		PageInfo<BankAccountInfo> resPage = memberservice.selectbankAcountList(info, page);
		js.setData(resPage);
		return js;
	}

	@RequestMapping("/addBank")
	public JsonResult addBank(@RequestBody BankAccountInfo info, HttpServletRequest request) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.insertBankAccount(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			} else {
				MessagePushTimer.withdrawalAccountList.add(info.getGameAccount());
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-提款账户管理-绑定银行卡(用户账号)" + info.getGameAccount());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/delBank")
	public JsonResult delBank(@RequestBody BankAccountInfo info) {
		JsonResult js = new JsonResult();
		int i = memberservice.delBankAccount(info);
		if (i > 0) {
			MessagePushTimer.withdrawalAccountList.add(info.getGameAccount());
		}
		js.setData(i);
		return js;
	}

	@RequestMapping("/updateBank")
	public JsonResult updateBank(@RequestBody BankAccountInfo info) {
		JsonResult js = new JsonResult();
		int i = memberservice.updateBankAccount(info);
		if (i > 0) {
			MessagePushTimer.withdrawalAccountList.add(info.getGameAccount());
		}
		js.setData(i);
		return js;
	}

	@RequestMapping("/querybankbyId")
	public JsonResult querybankbyId(@RequestBody BankAccountInfo info) {
		JsonResult js = new JsonResult();
		BankAccountInfo rinfo = memberservice.selectbackAcountInfo(info);
		js.setData(rinfo);
		return js;
	}

	// Bo821500515
	/************* 用户列表 **************************************/

	/**
	 * 获取在线用户
	 * 
	 * @param page
	 * @return
	 */

	@RequestMapping("/onlineUserList")
	public JsonResult onlineUserList(@RequestBody PageInfo<PlayAccountInfo> page) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PlayAccountInfo info = page.getInfo();
			PageInfo<PlayAccountInfo> list = memberservice.selectonlineuser(info, page);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/gameUserList")
	public JsonResult gameUserList(@RequestBody PageInfo<PlayAccountInfo> page) {
		PlayAccountInfo info = page.getInfo();
		if (info == null)
			info = new PlayAccountInfo();
		JsonResult js = new JsonResult();
		PageInfo<PlayAccountInfo> resPage = memberservice.selectplayAcountList(info, page);
		js.setData(resPage);
		return js;
	}

	@RequestMapping("/updateplayremarks")
	public JsonResult updateplayremarks(@RequestBody PlayAccountInfo info) {
		JsonResult js = new JsonResult();
		int i = memberservice.updateplayremarks(info);
		js.setData(i);
		return js;
	}

	@RequestMapping("/exportExcel")
	public JsonResult exportExcel(@RequestBody PlayAccountInfo info) {
		JsonResult js = new JsonResult();
		List<PlayAccountInfo> list = memberservice.exportExcel(info);
		js.setData(list);
		return js;
	}

	@RequestMapping("/updategameUsernotUse")
	public JsonResult updategameUsernotUse(@RequestBody PlayAccountInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updatePlayAccountNotUse(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			} else {
				MessagePushTimer.disableUserIdList.add(info.getAccountName());
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			PlayAccountInfo pl = memberservice.selectplayaccountbyId(info.getId());

			String accountStr = pl.getAccount();

			accountStr = accountStr.substring(0, accountStr.length() - 1);
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "会员管理-用户列表-账号冻结-(单)冻结" + accountStr);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updategameUserUse")
	public JsonResult updategameUserUse(@RequestBody PlayAccountInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updatePlayAccountUse(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			} else {
				MessagePushTimer.enableUserIdList.add(info.getAccountName());
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			PlayAccountInfo pl = memberservice.selectplayaccountbyId(info.getId());

			String accountStr = pl.getAccount();

			accountStr = accountStr.substring(0, accountStr.length() - 1);
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "会员管理-用户列表-账号冻结-(单)解封" + accountStr);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 账号冻结
	 * 
	 * @param info
	 * @param request
	 * @return
	 */
	@RequestMapping("/updategameUsernotUsepl")
	public JsonResult updategameUsernotUsepl(@RequestBody List<PlayAccountInfo> info, HttpServletRequest request) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int[] ids = info.stream().mapToInt(PlayAccountInfo::getId).toArray();
			int i = memberservice.updatePlayAccountNotUsepl(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			} else {
				for (PlayAccountInfo obj : info) {
					MessagePushTimer.disableUserIdList.add(obj.getAccountName());
				}
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			String accountStr = "";
			for (int j = 0; j < ids.length; j++) {
				PlayAccountInfo pl = memberservice.selectplayaccountbyId(ids[j]);

				if (pl == null)
					continue;
				accountStr += pl.getAccount() + ",";
			}
			accountStr = accountStr.substring(0, accountStr.length() - 1);
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-用户列表-账号冻结-(多)冻结账号" + accountStr);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updategameUserUsepl")
	public JsonResult updategameUserUsepl(@RequestBody List<PlayAccountInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int[] ids = info.stream().mapToInt(PlayAccountInfo::getId).toArray();
			int i = memberservice.updatePlayAccountUsepl(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			} else {
				for (PlayAccountInfo obj : info) {
					MessagePushTimer.enableUserIdList.add(obj.getAccountName());
				}
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			String accountStr = "";
			for (int j = 0; j < ids.length; j++) {
				PlayAccountInfo pl = memberservice.selectplayaccountbyId(ids[j]);

				if (pl == null)
					continue;
				accountStr += pl.getAccount() + ",";
			}
			accountStr = accountStr.substring(0, accountStr.length() - 1);
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-用户列表-账号冻结-(多)账号解封" + accountStr);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/gameUserChangeamout")
	public JsonResult gameUserAddamout(@RequestBody List<AcountReset> list) {
		JsonResult js = new JsonResult();
		int i = memberservice.insertacountReset(list);
		js.setData(i);
		return js;
	}

	/****************************** 行为记录 ******/

	@RequestMapping("/gamebehaviorList")
	public JsonResult gamebehaviorList(@RequestBody PageInfo<BehaviorInfo> page) {
		String startTime = null;
		String endTime = null;
		BehaviorInfo info = page.getInfo();
		if (info != null) {
			startTime = info.getStartTime();
			endTime = info.getEndTime();
		}
		JsonResult js = new JsonResult();
		PageInfo<BehaviorInfo> resPage = memberservice.selectbehaviorInfoList(page.getInfo().getGameAccount(), page,
				startTime, endTime);
		js.setData(resPage);
		return js;
	}

	// 存款记录
	@RequestMapping("/gamedepositList")
	public JsonResult gamedepositList(@RequestBody PageInfo<DepositInfo> page) {
		JsonResult js = new JsonResult();
		PageInfo<DepositInfo> resPage = memberservice.selectdepositInfoList(page);
		js.setData(resPage);
		return js;
	}

	// 提现记录
	@RequestMapping("/gamereflectList")
	public JsonResult gamereflectList(@RequestBody PageInfo<ReflectInfo> page) {
		JsonResult js = new JsonResult();
		PageInfo<ReflectInfo> resPage = memberservice.selectreflectInfoInfoList(page);
		js.setData(resPage);
		return js;
	}

	// 提现记录
	@RequestMapping("/selectrunwaterList")
	public JsonResult selectrunwaterList(@RequestBody PageInfo<RunWaterInfo> page) {
		JsonResult js = new JsonResult();
		JSONObject obj = memberservice.selectallrunwaterInfo(page);
		js.setData(obj);
		return js;
	}

}
